﻿namespace MicroCloud.Api.Systems.Controllers
{
    #region "邮件"
    /// <summary>
    /// 邮件
    /// </summary>
    [LoggedIn]
    [Description("邮件")]
    public class EmailController : SystemsControllerBase
    {
        #region "属性"
        #region "获取 Email接收功能"
        /// <summary>
        /// 获取 Email接收功能
        /// </summary>
        protected IEmailReceiver EmailReceiver => ServiceProvider.GetRequiredService<IEmailReceiver>();
        #endregion
        #region "获取 Email发送功能"
        /// <summary>
        /// 获取 Email发送功能
        /// </summary>
        protected IEmailSender EmailSender => ServiceProvider.GetRequiredService<IEmailSender>();
        #endregion

        #endregion

        #region "Action方法"
        #region "接收邮件"
        /// <summary>
        /// 接收邮件
        /// </summary>
        /// <param name="options">邮件配置信息</param>
        /// <returns>JSON操作结果</returns>
        [HttpPost]
        [NonFunction]
        [Description("接收")]
        public virtual async Task<AjaxResult> Receive(EmailReceiverOptions options = null)
        {
            EmailReceiverOptions emailReceiver = null;
            if (options?.Host.IsMissing() == false && options?.UserName.IsMissing() == false && options?.Password.IsMissing() == false)
            {
                emailReceiver = options;
            }
            var messages = await EmailReceiver.ReceiveEmailAsync(emailReceiver);
            return AjaxResult.Success(messages);
        }
        #endregion
        #region "发送邮件"
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="options">邮件发送配置</param>
        /// <returns>JSON操作结果</returns>
        [HttpPost]
        [NonFunction]
        [Description("发送")]
        public virtual async Task<AjaxResult> Send(EmailSendOptions options)
        {
            EmailSenderOptions emailSender = null;
            if (options.Sender?.Host.IsMissing() == false && options.Sender?.UserName.IsMissing() == false && options.Sender?.Password.IsMissing() == false)
            {
                emailSender = options.Sender;
            }
            Check.NotNull(options.SendDtos, nameof(options.SendDtos));
            await EmailSender.SendEmailAsync(emailSender, options.SendDtos);
            return AjaxResult.Success();
        }
        #endregion

        #endregion

    }
    #endregion

    #region "电子邮件发送配置"
    /// <summary>
    /// 电子邮件发送配置
    /// </summary>
    public class EmailSendOptions
    {
        #region "属性"
        #region "获取或设置 发送功能配置"
        /// <summary>
        /// 获取或设置 发送功能配置
        /// </summary>
        public EmailSenderOptions Sender { get; set; }
        #endregion
        #region "获取或设置 邮件发送DTO集合"
        /// <summary>
        /// 获取或设置 邮件发送DTO集合
        /// </summary>
        public EmailSendDto[] SendDtos { get; set; }
        #endregion

        #endregion

    }
    #endregion

}
